package com.project.forlearningdemo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.project.forlearningdemo.constant.ResponseTool;
import com.project.forlearningdemo.dao.ReportResourceMapper;
import com.project.forlearningdemo.pojo.vo.ResponseInfo;
import com.project.forlearningdemo.service.ReportResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReportResourceServiceImpl implements ReportResourceService {
    @Autowired
    private ReportResourceMapper reportResourceMapper;

    @Override
    public String addReportResource(int userId, int resourceId, String report) {
        JSONObject obj = JSONObject.parseObject(report);
        String reason = obj.getString("reason");
        String reportDetails = obj.getString("reportDetails");

        if(resourceId <= 0 || userId <= 0)
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.DATA_ERROR.code, ResponseTool.DATA_ERROR.msg, false));

        //查询是否已经举报了
        if(reportResourceMapper.queryReportResource(userId, resourceId)!=null)
            return JSON.toJSONString(new ResponseInfo<>(500, "您已经举报过了", false));

        if(reportResourceMapper.addReportResource(resourceId, userId, reason, reportDetails))
            return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_SUCCESS.code, ResponseTool.ADD_SUCCESS.msg, true));

        return JSON.toJSONString(new ResponseInfo<>(ResponseTool.ADD_FAIL.code,  ResponseTool.ADD_FAIL.msg, false));
    }
}
